﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using ServerSide.Common;
using ServerSide.EFCoreContext;
using ServerSide.Model;
using ServerSide.Service;
using ServerSide.Service.Interface;
using ServerSide.utils;

namespace ServerSide.Controllers
{
    [ApiController]
    [Route("userLoginapi/[action]")]
    public class UserLoginController:ControllerBase
    {
        private readonly IUserLoginService _userLoginService;
        private readonly IMenuService _menuService;
        private readonly MyDBContext _myDBContext;
        private readonly TokenUtil _tokenUtil;
        
        public UserLoginController(IUserLoginService userLoginService,MyDBContext myDBContext, TokenUtil tokenUtil, IMenuService menuService)
        {
            _myDBContext = myDBContext;
            _tokenUtil = tokenUtil;
            _userLoginService = userLoginService;
            _menuService = menuService;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost(Name = "userLogin")]
        public Result<Object> UserLogin(object? obj)
        {
            User user = JsonConvert.DeserializeObject<User>(obj.ToString());

            if (user.UserName != null && user.Password != null)
            {
                User? readUser = _myDBContext.User.Where(
                    item => item.UserName == user.UserName &&
                    item.Password == user.Password
                    ).FirstOrDefault();
                if (readUser != null)
                {
                    if (readUser.Enable == 1)
                    {
                        //个人信息完整
                        bool personalInfoResult = _userLoginService.JudgingPersonalInfo(readUser);
                        //获取用户权限菜单
                        List<Menu> menus = _menuService.GetMenuById(readUser.Id);
                        //生成token
                        string tokenResult = _tokenUtil.GenerateToken();
                        //设置校验时间
                        bool isOk = _tokenUtil.SetTokenValidityTime(readUser, tokenResult);
                        if (isOk)
                        {
                            var ResultData = new
                            {
                                menu = menus,
                                message = "登录成功",
                                personalInfo = personalInfoResult,
                                token = tokenResult,
                                UserId = readUser.Id
                            };
                            return Result<Object>.Success(ResultData);
                        }
                    }
                    return Result<Object>.Error("501", "用户被禁用,请联系管理员");
                }
                return Result<Object>.Error("501", "用户名或密码错误");
            }
            return Result<Object>.Error("501", "用户名或密码不可为空");
        }


        //[HttpGet]
        //public String Get()
        //{
        //    Role role = new Role()
        //    {
        //        Name = "admin",
        //        Description = "管理员",
        //        CreateBy = "admin",
        //        RoleSort = 1,
        //        Enable = 1,
        //        CreateTime = GetCurrentTime(),
        //        UpdateTime = GetCurrentTime(),
        //    };

        //    _myDBContext.Role.Add(role);
        //    _myDBContext.SaveChanges();
        //    return "成功";
        //}

        //public String GetCurrentTime()
        //{
        //    DateTime currentTime = DateTime.Now;
        //    return currentTime.ToString("yyyy-MM-dd HH:mm:ss");
        //}
    }
}
